package de.jmda.gen.java.impl;

import javax.validation.constraints.NotNull;

import de.jmda.gen.java.DeclaredFieldGenerator;
import de.jmda.gen.java.DeclaredStaticFieldGenerator;
import de.jmda.gen.java.StaticFieldGenerator;

public class DefaultStaticFieldGenerator
		extends AbstractFieldGenerator
		implements StaticFieldGenerator
{
	private DeclaredStaticFieldGenerator generator;

	public DefaultStaticFieldGenerator()
	{
		this(null);
	}
	
	public DefaultStaticFieldGenerator(
      DeclaredStaticFieldGenerator generator)
  {
	  super(notNull(generator));
	  
	  setDeclaredFieldGenerator(getDeclaredFieldGenerator());
  }

	@Override
  public DeclaredFieldGenerator getDeclaredFieldGenerator()
  {
	  return generator;
  }

	/**
	 * @param generator
	 * @throws IllegalArgumentException if <code>generator</code> is not an
	 *         Static of {@link DeclaredStaticFieldGenerator}
	 */
	@Override
  public void setDeclaredFieldGenerator(DeclaredFieldGenerator generator)
  {
	  if (generator instanceof DeclaredStaticFieldGenerator)
		{
			this.generator = (DeclaredStaticFieldGenerator) generator;
		}
	  else
	  {
	  	throw new IllegalArgumentException(
	  			"generator has to be an Static of " +
	  			DeclaredStaticFieldGenerator.class.getName());
	  }

	  super.setDeclaredFieldGenerator(this.generator);
  }

	@Override
  public DeclaredStaticFieldGenerator getDeclaredStaticFieldGenerator()
  {
	  return generator;
  }

	@Override
  public void setDeclaredStaticFieldGenerator(
      DeclaredStaticFieldGenerator generator)
  {
		setDeclaredFieldGenerator(generator);
  }

	@NotNull 
	private static DeclaredFieldGenerator notNull(
      DeclaredStaticFieldGenerator generator)
  {
		if (generator == null)
		{
			generator = new DefaultDeclaredStaticFieldGenerator();
		}

		return generator;
  }
}